package com.project.class4_8.proxy.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ServiceProxy implements InvocationHandler {
    private Object targetObj;
    public ServiceProxy(Object targetObj){
        this.targetObj=targetObj;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(
                targetObj.getClass().getClassLoader(),
                targetObj.getClass().getInterfaces(),
                this
        );

    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Field [] array= targetObj.getClass().getDeclaredFields();
        for (Field f:array){
            if (f.isAnnotationPresent(Auto.class)){
                Object obj=DaoFactory.getObject(f.getType());
                f.setAccessible(true);
                f.set(targetObj,obj);
            }
        }
        method.invoke(targetObj,args);

        return null;
    }
}
